Controllor = class()

function Controllor:init(x,y,width,a)
    self.x = x
    self.y = y
    self.width = width
    self.a = a
    self.bsize = (width - a * 2) / 3
    self:resetAllTouch()
    self.mesh = mesh()
    self.flying = false
    self.advWalking = false
end

function Controllor:draw()
    local s = self.bsize
    local c = s + self.a
    local r = s / 2 + self.a
    local e = self.a / 3
    pushMatrix()
    pushStyle()
    noStroke()
    rectMode(CENTER)
    translate(self.x,self.y)
    local uc,pc = color(234, 233, 230, 201),color(95, 88, 88, 233)
    if self.keys.a then fill(pc) else fill(uc) end
    rect(-c,0,s,s)
    if self.keys.d then fill(pc) else fill(uc) end
    rect(c,0,s,s)
    if self.keys.w then fill(pc) else fill(uc) end
    rect(0,c,s,s)
    if self.keys.s then fill(pc) else fill(uc) end
    rect(0,-c,s,s)
    if self.keys.space then fill(pc) else fill(uc) end
    rect(0,0,s,s)
    self.mesh.vertices = {
    vec2(- r - e,s / 2 - e),vec2(- r - e,-s / 2 + e),vec2(-c - s / 2 + e),
    vec2(r + e,s / 2 - e),vec2(r + e,-s / 2 + e),vec2(c + s / 2 - e,0),
    vec2(s / 2 - e,r + e),vec2(-s / 2 + e,r + e),vec2(0,c + s / 2 - e),
    vec2(s / 2 - e,-r - e),vec2(-s / 2 + e,- r - e),vec2(0,-c - s / 2 + e),
    }
    local c = color(104, 86, 86, 229)
    fill(c)
    self.mesh:setColors(c)
    if player.mode == Player.creativeMode then
        ellipseMode(CENTER)
        ellipse(0,0,s / 2 - e)
    else
        rect(0,0,s - 2 * e,s - 2 * e)
    end
    self.mesh:draw()
    popStyle()
    popMatrix()
end


function Controllor:touched(touch)
    tx,ty = touch.x,touch.y
    local s = self.bsize
    local c = s + self.a
    local r = s / 2 + self.a
    local e = self.a / 3
    if touch.state == ENDED then
        for k,v in pairs(self.touchid) do
            if touch.id == v then
                if k == "a" then
                    self.keys.a = false
                elseif k == "d" then
                    self.keys.d = false
                elseif k == "s" then
                    self.keys.s = false
                elseif k == "w" then
                    self.keys.w = false
                    self.keys.allways = false
                    self.keys.left = false
                    self.keys.right = false
                    self.keys.forward = false
                    self.keys.backward = false
                    self.keys.jump = false
                    self.hasJumped = false
                elseif k == "space" then
                    self.keys.space = false
                    self.flying = false
                    self.keys.down = false
                    self.keys.up = false
                    self.hasJumped = false
                end
                self.touchid[k] = 0
            end
        end
    end
    if touch.state == BEGAN then
        if self:isInButton(tx,ty,self.x - c,self.y,s,s) then
            self.keys.a = true
            self.touchid.a = touch.id
        elseif self:isInButton(tx,ty,self.x + c,self.y,s,s) then
            self.keys.d = true
            self.touchid.d = touch.id
        elseif self:isInButton(tx,ty,self.x,self.y - c,s,s) then
            self.keys.s = true
            self.touchid.s = touch.id
        elseif self:isInButton(tx,ty,self.x,self.y + c,s,s) then
            self.keys.w = true
            self.touchid.w = touch.id
        elseif self:isInButton(tx,ty,self.x,self.y,s,s) then
            self.keys.space = true
            self.touchid.space = touch.id
            self.hasJumped = false
            if player.mode == Player.creativeMode then
                self.flying = true
            end
        else
            return false
        end
    elseif touch.state == ENDED then
        if self:isInButton(tx,ty,self.x - c,self.y,s,s) then
            self.keys.a = false
            self.touchid.a = 0
        elseif self:isInButton(tx,ty,self.x + c,self.y,s,s) then
            self.keys.d = false
            self.touchid.d = 0
        elseif self:isInButton(tx,ty,self.x,self.y - c,s,s) then
            self.keys.s = false
            self.touchid.s = 0
            if self.flying then
                self.keys.down = false
            end
        elseif self:isInButton(tx,ty,self.x,self.y + c,s,s) then
            self.keys.w = false
            self.touchid.w = 0
            if self.flying then
                self.keys.up = false
            end
            self.keys.allways = false
            self.keys.left = false
            self.keys.right = false
            self.keys.forward = false
            self.keys.backward = false
            self.keys.jump = false
            self.hasJumped = false
        elseif self:isInButton(tx,ty,self.x,self.y,s,s) and touch.tapCount == 1 then
            self.keys.space = false
            self.touchid.space = 0
            self.hasJumped = false
            if player.mode == Player.creativeMode then
                self.flying = false
                self.keys.up = false
                self.keys.down = false
            end
        elseif self:isInButton(tx,ty,self.x,self.y,s,s) and touch.tapCount == 2 then
            player:changeMode()
        else
            return false
        end
    else
        if self.flying and touch.id == self.touchid.space then
            if ty > self.y + s / 2 then
                self.keys.up = true
            else
                self.keys.up = false
            end
            if ty < self.y - s / 2 then
                self.keys.down = true
            else
                self.keys.down = false
            end
        end
        if self.keys.w and touch.id == self.touchid.w then
            if self.keys.allways == false and not self:isInButton(tx,ty,self.x,self.y + c,s,s) then
                self.keys.allways = true
            elseif self.keys.allways then
                if ty > self.y + s / 2 then
                    self.keys.forward = true
                elseif ty < self.y - s / 2 then
                    self.keys.forward = false
                    self.keys.backward = true
                else
                    self.keys.forward = false
                    self.keys.backward = false
                end
                if tx > self.x + s / 2 then
                    self.keys.right = true
                elseif tx < self.x - s / 2 then
                    self.keys.left = true
                else
                    self.keys.right = false
                    self.keys.left = false
                end
                if tx < self.x + s / 2 and tx > self.x - s / 2 and ty < self.y + s / 2 and ty > self.y - s / 2 then
                    self.keys.forward = true
                    self.keys.jump = true
                else
                    self.keys.jump = false
                    self.hasJumped = false
                end
            end
        end
        return false
    end
    return true
end

function Controllor:isInButton(tx,ty,x,y,w,h)
    return (tx >= x - w / 2) and (ty >= y - w / 2) and (tx <= x + w / 2) and (ty <= y + h / 2)
end

function Controllor:resetAllTouch()
    self.keys = {
    w,a,s,d = false,false,false,false,
    space = false,
    down,up = false,false,
    left,right = false,false,
    allways = false,
    forward,backward = false,false,
    jump = false
    }
    self.touchid = {
    w,a,s,d = 0,0,0,0,
    space = 0
    }
end
